package com.hupu.jrs.util

import android.content.Context
import android.preference.PreferenceManager
import com.hupu.jrs.Constants
import com.hupu.jrs.R


/**
 * Created by sll on 2015/5/16.
 */
object SettingPrefUtil {

    /**
     * 正文字体大小
     *
     * @return
     */
    var txtSizeResArr = intArrayOf(R.dimen.text_size_12, R.dimen.text_size_13, R.dimen.text_size_14, R.dimen.text_size_15, R.dimen.text_size_16, R.dimen.text_size_17, R.dimen.text_size_18, R.dimen.text_size_19, R.dimen.text_size_20)
    /**
     * 标题字体大小
     */
    var titleSizeResArr = intArrayOf(R.dimen.text_size_15, R.dimen.text_size_16, R.dimen.text_size_17, R.dimen.text_size_18, R.dimen.text_size_19, R.dimen.text_size_20, R.dimen.text_size_21, R.dimen.text_size_22, R.dimen.text_size_23)

    var offlineCountArr = intArrayOf(50, 100, 150, 200)

    fun getOffline(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("Offline", true)
    }

    fun getThemeIndex(context: Context): Int {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getInt("ThemeIndex", 9)
    }

    fun setThemeIndex(context: Context, index: Int) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putInt("ThemeIndex", index).apply()
    }

    fun setOffline(context: Context, isOffline: Boolean) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)

        prefs.edit().putBoolean("Offline", isOffline).apply()
    }

    fun getPicSavePath(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getString("PicSavePath", "gzsll")
    }

    fun setPicSavePath(context: Context, path: String) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putString("PicSavePath", path).apply()
    }

    fun getTextSize(context: Context): Int {
        return context.resources.getDimensionPixelSize(txtSizeResArr[getTextSizePref(context)])
    }

    fun getTitleSize(context: Context): Int {
        return context.resources.getDimensionPixelSize(titleSizeResArr[getTextSizePref(context)])
    }

    private fun getTextSizePref(context: Context): Int {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return Integer.parseInt(prefs.getString("pTextSize", "3"))
    }

    fun getThreadSort(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return if (Integer.parseInt(prefs.getString("pThreadSort", "0")) == 0)
            Constants.THREAD_TYPE_NEW
        else
            Constants.THREAD_TYPE_HOT
    }

    fun getNightModel(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pNightMode", false)
    }

    fun setNightModel(context: Context, nightModel: Boolean) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putBoolean("pNightMode", nightModel).apply()
    }

    fun getLoadPic(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pLoadPic", true)
    }

    fun getNotification(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pNotification", true)
    }

    fun getLoadOriginPic(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pLoadOriginPic", false)
    }

    fun getAutoUpdate(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pAutoUpdate", true)
    }

    fun getSingleLine(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("pSingleLine", false)
    }

    fun getSwipeBackEdgeMode(context: Context): Int {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return Integer.parseInt(prefs.getString("pSwipeBackEdgeMode", "0"))
    }

    fun getOfflineCount(context: Context): Int {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return offlineCountArr[Integer.parseInt(prefs.getString("pOfflineCount", "0"))]
    }

    fun getLoginUid(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getString("loginUid", "")
    }

    fun setLoginUid(context: Context, uid: String) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putString("loginUid", uid).apply()
    }

    fun setNeedExam(context: Context, needExam: Boolean) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putBoolean("needExam", needExam).apply()
    }

    fun isNeedExam(context: Context): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getBoolean("needExam", false)
    }

    fun getHuPuSign(context: Context): String {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        return prefs.getString("hupuSign", "HUPU_SALT_AKJfoiwer394Jeiow4u309")
    }

    fun setHuPuSign(context: Context, hupuSign: String) {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        prefs.edit().putString("hupuSign", hupuSign).apply()
    }
}
